package com.baijiayun.liveuibase;

import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.net.Uri;
import android.text.TextUtils;

import androidx.annotation.NonNull;

import com.baijia.bjydialog.MaterialDialog;
import com.baijiayun.livecore.context.LPConstants;
import com.baijiayun.liveuibase.ascamera.AsCameraActivity;


/**
 * Created by pengwenjian on 2020/11/16
 * Describe:
 */
public class LiveCameraWithUI {
    public static LPConstants.LPResolutionType defaultResolution = LPConstants.LPResolutionType.LOW;

    public static void enterRoom(@NonNull Context context, @NonNull String url, LiveSDKEnterRoomListener listener) {
        if (TextUtils.isEmpty(url)) {
            if (listener != null) {
                listener.onError("url is empty");
            }
            return;
        }
        Uri uri = Uri.parse(url);
        String replaceUserNumber = uri.getQueryParameter("replace_user_number");
        if (TextUtils.isEmpty(replaceUserNumber)) {
            replaceUserNumber = uri.getQueryParameter("replaceUserNumber");
        }
        if (TextUtils.isEmpty(replaceUserNumber)) {
            if (listener != null) {
                listener.onError("replace_user_number is empty");
            }
            return;
        }
        addDefaultLoginConflictCallback(context);
        Intent intent = new Intent(context, AsCameraActivity.class);
        intent.putExtra("url", url);
        context.startActivity(intent);
    }

    private static boolean isPad(Context context) {
        return (context.getResources().getConfiguration().screenLayout
                & Configuration.SCREENLAYOUT_SIZE_MASK)
                >= Configuration.SCREENLAYOUT_SIZE_LARGE;
    }

    public interface LiveSDKEnterRoomListener {
        void onError(String msg);
    }

    public static void setRoomExitListener(LPRoomExitListener listener) {
        AsCameraActivity.Companion.setRoomExitListener(listener);
    }


    public static void setEnterRoomConflictListener(RoomEnterConflictListener listener) {
        AsCameraActivity.Companion.setEnterRoomConflictListener(listener);
    }


    public interface RoomEnterConflictListener {
        void onConflict(Context context, LPConstants.LPEndType endType, LPRoomExitCallback callback);
    }

    public interface LPRoomExitListener {
        void onRoomExit(Context context, LPRoomExitCallback callback);
    }

    public interface LPRoomExitCallback {
        void exit();

        void cancel();
    }


    /**
     * 登录冲突默认弹框提示
     */
    private static void addDefaultLoginConflictCallback(Context context) {
        LiveCameraWithUI.setEnterRoomConflictListener((context12, endType, callback) -> {
            String message;
            switch (endType) {
                case iOS:
                    message = "iOS";
                    break;
                case PC_H5:
                case PC_HTML:
                    message = "PC网页";
                    break;
                case PC_Client:
                    message = "PC客户";
                    break;
                case PC_MAC_Client:
                    message = "MAC客户";
                    break;
                case Android:
                    message = "Android";
                    break;
                default:
                    message = endType.name();
                    break;
            }
            new MaterialDialog.Builder(context12)
                    .content(String.format(context.getString(R.string.base_live_login_conflict), message))
                    .positiveText(R.string.base_live_confirm)
                    .canceledOnTouchOutside(false)
                    .onPositive((dialog, which) -> callback.exit())
                    .show();
        });
    }
}
